//
//  KRAlarmViewController.m
//  KEREN
//
//  Created by MW on 2023/4/20.
//

#import "KRAlarmViewController.h"
#import "KRMessageNoticeCell.h"
#import "KRMessageNoticeDetailVC.h"
#import "KRNewMessageNoticeCell.h"
@interface KRAlarmViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArrM;
@property (nonatomic, assign) int index;

@end

@implementation KRAlarmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getmessageNotifyList];
}

- (void)messageNotifyupdate:(NSInteger)messageNotifyID{
   //v1/app/messageNotify/update
    WEAKSELF
    NSDictionary *parameters = @{@"id":@(messageNotifyID),@"flag":@(1)};
    [NetworkingTool POST:@"/v1/app/messageNotify/update" parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView reloadData];
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];
}

- (void)getmessageNotifyList{
    WEAKSELF
    NSDictionary *parameters = @{@"index":@(_index),@"size":@(20),@"message_type":@(2),@"time_stamp":@(_time_stamp)};
    [NetworkingTool POST:@"/v1/app/messageNotify/list" parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];
        [[KRAlartManager shareInstance] hidenLodingImageView];

        if([response[@"code"] intValue] == 0){
            if(weakSelf.index == 1){
                [weakSelf.dataArrM removeAllObjects];
            }
            if([response[@"data"][@"list"] isKindOfClass:[NSArray class]]){
                NSArray *dataArr = response[@"data"][@"list"];
                for (int i = 0; i < dataArr.count; i ++) {
                    NSDictionary *dict = dataArr[i];
                    [weakSelf.dataArrM addObject:dict];
                }
                if(weakSelf.dataArrM.count > 0){
                    weakSelf.tableView.tableHeaderView = nil;
                }else{
                    weakSelf.tableView.tableHeaderView = [[KRNodataView alloc] initWithFrame:CGRectZero];
                }
                if(dataArr.count < 20){
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                if (weakSelf.dataArrM.count == 0) {
                    [weakSelf.tableView.mj_footer setHidden:YES];
                }else{
                    [weakSelf.tableView.mj_footer setHidden:NO];
                }

                [weakSelf.tableView reloadData];

            }else{
                weakSelf.tableView.tableHeaderView = [[KRNodataView alloc] initWithFrame:CGRectZero];
                [weakSelf.tableView reloadData];
            }
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];
        [[KRAlartManager shareInstance] hidenLodingImageView];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //return self.dataArrM.count;
    return self.dataArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [UIView new];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KRNewMessageNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KRNewMessageNoticeCell"];
    //cell.iconImageView.image = [UIImage imageNamed:@"messge_gj_icon"];
    NSDictionary *dict = self.dataArrM[indexPath.section];
    cell.deviceNameLbl.text = dict[@"ebike_name"];
    cell.decLbl.text = dict[@"content"];
    cell.redView.layer.cornerRadius = 4;
    cell.redView.clipsToBounds = YES;
    cell.deviceNumbLbl.text = dict[@"frame_code"];
    if([dict[@"flag"] intValue] == 0){
        cell.redView.hidden = NO;
        cell.deviceNumbLbl.textColor = Color_7ACC00;
        cell.leftWidthConstraint.constant = 36;
    }else{
        cell.redView.hidden = YES;
        cell.leftWidthConstraint.constant = 16;
        cell.deviceNumbLbl.textColor = Color_86898E;
    }
  //  cell.decLbl.text = dict[@"content"];
    cell.timeLbl.text = [[DeteManager shareInstance] timeFormatHHmmss:[dict[@"created_at"] intValue]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    KRMessageNoticeDetailVC *vc = [[KRMessageNoticeDetailVC alloc] init];
    vc.type = 1;
    NSDictionary *dict = self.dataArrM[indexPath.section];
    vc.dict = dict;
    [self messageNotifyupdate:[dict[@"id"] integerValue]];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, self.view.bounds.size.height - 108 - STATUSBAR_HEIGHT - 60)];
        _tableView.backgroundColor = Main_BackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 104;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"KRNewMessageNoticeCell" bundle:nil] forCellReuseIdentifier:@"KRNewMessageNoticeCell"];
        WEAKSELF
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.index = 1;
            [self getmessageNotifyList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.index ++;
            [self getmessageNotifyList];
        }];
        _tableView.mj_footer.hidden = YES;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArrM{
    if(!_dataArrM){
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

@end
